package com.lvshou.medicalkit.base;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.trello.rxlifecycle3.android.ActivityEvent;
import com.trello.rxlifecycle3.components.support.RxAppCompatActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import androidx.annotation.DrawableRes;
import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.appcompat.widget.Toolbar;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.ObservableTransformer;

/**
 * 基类
 *
 * @param <P>
 */
public abstract class BaseActivity<P extends BasePresenter> extends RxAppCompatActivity implements BaseView {
    private P mPresenter;
    private Unbinder unbinder;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setContentView(getLayoutId());

        unbinder = ButterKnife.bind(this);
        //创建Presenter,交给子类去实现
        mPresenter = createPresenter();
        //让P层去绑定V
        if (mPresenter != null) {
            mPresenter.attach(this);
        }
        //兼容5.0以下矢量图
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
        initView();
        initData();

        if (useEventBus()) {
            EventBus.getDefault().register(this);
        }

    }

    /**
     * 返回Activity的布局Id
     *
     * @return
     */
    protected abstract int getLayoutId();

    /**
     * 创建Presenter
     *
     * @return
     */
    protected abstract P createPresenter();

    /**
     * 初始化View
     */
    protected abstract void initView();

    /**
     * 在这里去请求数据
     */
    protected abstract void initData();

    public P getPresenter() {
        return mPresenter;
    }

    @Override
    public <T> ObservableTransformer<T, T> bindLifecycle() {
        return this.bindUntilEvent(ActivityEvent.DESTROY);//绑定到Activity的pause生命周期（在pause销毁请求）
    }

//    @Override
//    protected void attachBaseContext(Context base) {
//        super.attachBaseContext(AppLanguageUtils.attachBaseContext(base, AppLanguageUtils.getAppLanguage(base)));
//    }

    /**
     * 是否使用eventBus
     *
     * @return
     */
    protected boolean useEventBus() {
        return false;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.detach();
        }
        if (unbinder != null) {
            unbinder.unbind();
        }
        if (useEventBus()) {
            if (EventBus.getDefault().isRegistered(this)) {
                EventBus.getDefault().unregister(this);//注销eventBus
            }
        }
    }

    /**
     * 初始化 Toolbar
     *
     * @param toolbar
     */
    protected void initToolBar(Toolbar toolbar) {
        initToolBar(toolbar, false, false, "");
    }

    protected void initToolBar(Toolbar toolbar, boolean homeAsUpEnabled, boolean showTitleEnabled, int resTitle) {
        initToolBar(toolbar, homeAsUpEnabled, showTitleEnabled, resTitle != 0 ? getString(resTitle) : "");
    }

    protected void initToolBar(Toolbar toolbar, boolean homeAsUpEnabled, boolean showTitleEnabled, String title) {
        if (!TextUtils.isEmpty(title)) {
            toolbar.setTitle(title);
        }
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(homeAsUpEnabled);
        getSupportActionBar().setDisplayShowTitleEnabled(showTitleEnabled);
    }

    protected void setToolBarImageView(@IdRes int id, @DrawableRes int resId) {
        setToolBarImageView((ImageView) findViewById(id), resId);
    }

    protected void setToolBarTextView(@IdRes int id, @StringRes int resId) {
        setToolBarTextView((TextView) findViewById(id), resId);
    }

    protected void setToolBarTextView(@NonNull TextView toolbarTextView, @NonNull String text) {
        toolbarTextView.setText(text);
        setVisibility(toolbarTextView, true);
    }

    protected void setToolBarTextView(@NonNull TextView toolbarTextView, @StringRes int stringId) {
        toolbarTextView.setText(stringId);
        setVisibility(toolbarTextView, true);
    }

    protected void setToolBarImageView(@NonNull ImageView toolbarImageView, @DrawableRes int resId) {
        toolbarImageView.setImageResource(resId);
        setVisibility(toolbarImageView, true);
    }

    protected void setVisibility(@NonNull View view, boolean isVisible) {
        view.setVisibility(isVisible ? View.VISIBLE : View.GONE);
    }

    @Subscribe
    public void finishEvent(String event) {

    }
}
